package com.isoft.illustrationsdraw.controller;

import com.isoft.illustrationsdraw.Constants;
import com.isoft.illustrationsdraw.brief.BriefIllustrationInfo;
import com.isoft.illustrationsdraw.service.SearchService;
import com.isoft.illustrationsdraw.service.UserDTOService;
import com.isoft.illustrationsdraw.service.JWTService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/api/search")
public class SearchController {

    private final SearchService searchService;
    private final JWTService jwtService;
    private final UserDTOService userDTOService;

    public SearchController(SearchService searchService, JWTService jwtService, UserDTOService userDTOService) {
        this.jwtService = jwtService;
        this.searchService = searchService;
        this.userDTOService = userDTOService;
    }

    // 根据标签搜索插画
    @GetMapping("/illustrationsByTag")
    public ResponseEntity<?> searchIllustrationByTag(@RequestParam String token, @RequestParam String tag, @RequestParam Integer page) {
        if (!jwtService.validateToken(token)) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(Constants.Error.INVALID_TOKEN); // 401 Unauthorized
        }
        
        if (tag == null || tag.isEmpty() || page < 1) {
            return ResponseEntity.badRequest().body("Invalid tag or page number.");
        }
        
        Long userId = jwtService.getUserIdFromToken(token);

        try {
            List<BriefIllustrationInfo> illustrations = searchService.searchIllustrationByTag(userId, tag, null, page);
            return ResponseEntity.ok(illustrations);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error retrieving illustrations by tag: " + e.getMessage());
        }
    }

    // 根据关键字搜索插画
    @GetMapping("/illustrationsByKeywords")
    public ResponseEntity<?> searchIllustrationByKeywords(@RequestParam String token, @RequestParam String keywords, @RequestParam Integer page) {
        if (!jwtService.validateToken(token)) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(Constants.Error.INVALID_TOKEN); // 401 Unauthorized
        }
        
        if (keywords == null || keywords.isEmpty() || page < 1) {
            return ResponseEntity.badRequest().body("Invalid keywords or page number.");
        }

        Long userId = jwtService.getUserIdFromToken(token);

        try {
            List<BriefIllustrationInfo> illustrations = searchService.searchIllustrationByKeyworks(userId, keywords, page);
            return ResponseEntity.ok(illustrations);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error retrieving illustrations by keywords: " + e.getMessage());
        }
    }

    @GetMapping("/illustrationsByAuthorName")
    public ResponseEntity<?> searchIllustrationByAuthorName(@RequestParam String token, @RequestParam String authorName, @RequestParam Integer page) {
        if (!jwtService.validateToken(token)) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(Constants.Error.INVALID_TOKEN); // 401 Unauthorized
        }

        if (authorName == null || authorName.isEmpty() || page < 1) {
            return ResponseEntity.badRequest().body("Invalid authorName or page number.");
        }

        
        Long userId = jwtService.getUserIdFromToken(token);

        try {
            List<BriefIllustrationInfo> illustrations = userDTOService.getUserWorks(userId,null, page, authorName);
            return ResponseEntity.ok(illustrations);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error retrieving illustrations by authorName: " + e.getMessage());
        }

    }
    

    // // 根据用户名搜索用户
    // @GetMapping("/users")
    // public ResponseEntity<?> searchUserByName(@RequestParam String name, @RequestParam Integer page) {
    //     if (name == null || name.isEmpty() || page < 1) {
    //         return ResponseEntity.badRequest().body("Invalid name or page number.");
    //     }

    //     try {
    //         List<BriefIllustrationInfo> users = searchService.searchUserByName(name, page);
    //         return ResponseEntity.ok(users);
    //     } catch (Exception e) {
    //         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error retrieving users by name: " + e.getMessage());
    //     }
    // }

}
